# Contributor: Jakub Jirutka <jakub@jirutka.cz>
# Maintainer: Jakub Jirutka <jakub@jirutka.cz>
pkgname=java-jna
_pkgname=${pkgname#java-}
pkgver=5.6.0
pkgrel=0
pkgdesc="JNA provides Java programs easy access to native shared libraries."
url="https://github.com/java-native-access/jna"
arch="all !mips !mips64"
license="Apache-2.0 LGPL-2.1"
depends="openjdk8-jre-base"
makedepends="apache-ant autoconf automake libffi-dev>=3.2 libtool openjdk8"
subpackages="$pkgname-native"
source="$pkgname-$pkgver.tar.gz::https://github.com/java-native-access/$_pkgname/archive/$pkgver.tar.gz
	0001-jar-without-natives.patch
	"
builddir="$srcdir/$_pkgname-$pkgver"

prepare() {
	default_prepare

	cd "$builddir"

	# Disable Java AWT support (requires some X libs).
	sed -i -E "s/^(CDEFINES=.*)$/\1 -DNO_JAWT/g" native/Makefile

	# Remove bundled libffi sources, we're gonna use headers form libffi-dev.
	rm -r native/libffi

	# Remove some bundled JARs.
	rm -r dist
	find lib/native -name "*.jar" -exec rm {} +
}

build() {
	cd "$builddir"

	ant clean

	# Parallel build doesn't work here!
	MAKEFLAGS="-j1" \
		ant native -Drelease=true -Ddynlink.native=true \
		&& ant jar -Domit-jnilib=true
}

package() {
	depends="$depends $pkgname-native"

	install -m644 -D "$builddir"/build/jna.jar \
		"$pkgdir"/usr/share/java/$_pkgname-$pkgver.jar
	ln -sf $_pkgname-$pkgver.jar "$pkgdir"/usr/share/java/$_pkgname.jar
}

native() {
	local soname="libjnidispatch.so"

	cd "$builddir"

	local sover="$(sed -En 's/^JNA_JNI_VERSION=([^ ]+).*/\1/p' native/Makefile)"
	[ -n "$sover" ]

	install -m755 -D build/native-linux-*/$soname \
		"$subpkgdir"/usr/lib/$soname.$sover
	ln -sf $soname.$sover "$subpkgdir"/usr/lib/$soname
}

sha512sums="645dd13f1ab8b36277fcd2af1557ca9fe15a72aaf78564ed9c2fb027a1b7d4fa93731aa2986d08fabb5af472f79b6cd1d2a260bddb966eac7e50dcd9d3158729  java-jna-5.6.0.tar.gz
600dbd3fdc2b8413599e93650654acdbe43fb49a271e718cb043836231b628914eb141100665c456464fb5260caea6db960a3810691c97d868290340d5524ec3  0001-jar-without-natives.patch"
